package com.stylefeng.guns.modular.business.controller;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.enums.StatusEnum;
import com.stylefeng.guns.common.page.PageReq;
import com.stylefeng.guns.common.persistence.dao.GameMapper;
import com.stylefeng.guns.common.persistence.dao.HistoryMapper;
import com.stylefeng.guns.common.persistence.model.Game;
import com.stylefeng.guns.common.persistence.model.History;
import com.stylefeng.guns.modular.business.service.IGameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 弹幕管理控制器
 *
 * @author fengshuonan
 * @Date 2018-08-18 15:57:09
 */
@Slf4j
@Controller
@RequestMapping("/game")
public class GameController extends BaseController {

    private String PREFIX = "/business/game/";

    @Autowired
    private GameMapper gameMapper;

    @Autowired
    private HistoryMapper historyMapper;

    @Autowired
    private IGameService gameService;

    /**
     * 跳转到弹幕管理首页
     */
    @RequestMapping("")
    public String index(@RequestParam(required = false) Integer type, Model model) {
        model.addAttribute("statusMap", StatusEnum.getKeyValue());
        model.addAttribute("titleMap", gameMapper.selectAll());
        model.addAttribute("matchType", type);
        return PREFIX + "game.html";
    }

    /**
     * 跳转
     */
    @RequestMapping("/view/{mid}")
    public String view(@PathVariable String mid, HttpServletRequest request, Model model) {

        Map<String, Object> param = new HashMap<>();
        Game game = gameMapper.findByMid(mid);
        String type = request.getParameter("type");
        param.put("mid", mid);
        param.put("type", request.getParameter("type"));
        if ("awaycomp".equals(type)) {
            param.put("team", game.getKed());
        } else {
            param.put("team", game.getZhud());
        }
        model.addAllAttributes(param);

        return PREFIX + "game_view.html";
    }

    /**
     * 跳转到查看统计数据页面
     */
    @RequestMapping("/viewStat/{mid}")
    public String viewStat(@PathVariable String mid, Model model) {

        Game game = gameService.viewStat(mid);
        model.addAttribute("statMap", game);
        return PREFIX + "game_stat.html";
    }

    /**
     * 获取弹幕管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Game game) {

        List<Game> gameList = gameService.list(game);
        return gameList;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/view_list")
    @ResponseBody
    public Object detail(String mid, String type) {

        List<History> result = Lists.newArrayList();
        if (mid != null) {
            PageReq params = defaultPage();
            PageHelper.offsetPage(params.getOffset(), params.getLimit());
            result = historyMapper.findbyMidAndcompType(mid, type);
        }
        return packForBT(result);
    }


    /**
     * 开启爬虫
     */
    @RequestMapping(value = "/start")
    @ResponseBody
    public Object start(Integer type) {
        gameService.start(type);
        return SUCCESS_TIP;
    }

    /**
     * 手动更新未结束数据
     *
     * @return
     */
    @RequestMapping(value = "/clear")
    @ResponseBody
    public Object update() {

        gameService.update();

        return SUCCESS_TIP;
    }

}
